import { Service } from 'egg';

export default class Advertise extends Service {
    /**
    * 获取电影分类类型
    * */
    public async getAdConfig() {
        const { ctx, app } = this;
        const { Joi } = app;

        let schema = Joi.object({
            setting_name: Joi.string().required(),
        });
        let { error } = ctx.validate(schema, ctx.query);
        if (error) {
            ctx.status = 403;
            return { error: error.message };
        };
        let res = await app.mysql.get('Yusup').select('settings', {
            where: { setting_name: ctx.query.setting_name, user: app.dbName },
            select: 'setting_content'
        });
        if (res[0]) {
            return {
                code: 200,
                data: JSON.parse(res[0].setting_content)
            }
        } else {
            ctx.status = 500;
        }
    }
    /**
    * 保存广告配置信息
    * */
    public async setAdConfig() {
        const { ctx, app } = this;
        const { Joi } = app;

        let schema = Joi.object({
            setting_name: Joi.string().required(),
            list: Joi.array(),
            config: Joi.object()
        });
        let { error } = ctx.validate(schema, ctx.request.body);
        if (error) {
            ctx.status = 403;
            return { error: error.message };
        };
        let { setting_name } = ctx.request.body;
        //? 保存滚动广告配置
        if (setting_name == 'movieSwiperConfig' || setting_name == 'tvSwiperConfig') {
            let list = ctx.request.body.list || [];
            for (const item of list) {
                item.type = Number(item.type);
                item.xh = Number(item.xh);
                let scm = Joi.object({
                    type: Joi.number().required(),
                    xh: Joi.number().required(),
                    url: Joi.string().required(),
                    image: Joi.string().required(),
                });
                let { error } = ctx.validate(scm, item);
                if (error) {
                    ctx.status = 403;
                    return { error: error.message };
                }
            };
            await app.mysql.get('Yusup').update('settings', { setting_content: JSON.stringify(list) }, {
                where: {
                    user: app.dbName,
                    setting_name
                }
            });
            return {
                code: 200,
                data: 'success'
            }
        }
        //? 播放页广告配置
        else if (setting_name == 'playerPageAdConfig') {
            let { config } = ctx.request.body
            let schema = Joi.object({
                status: Joi.bool().required(),
                name: Joi.string(),
                url: Joi.string().required(),
                image: Joi.string().required(),
                type: Joi.number().required(),
                title: Joi.string(),
            });
            let { error } = ctx.validate(schema, config);
            if (error) {
                ctx.status = 403;
                return { error: error.message };
            };
            await app.mysql.get('Yusup').update('settings', { setting_content: JSON.stringify(config) }, {
                where: {
                    user: app.dbName,
                    setting_name
                }
            });
            return {
                code: 200,
                data: 'success'
            }
        }
        //? 保存分类配置
        else if (setting_name == 'classifyList') {
            let list = ctx.request.body.list || [];
            for (const item of list) {
                item.type = Number(item.type);
                item.xh = Number(item.xh);
                let scm = Joi.object({
                    xh: Joi.number().required(),
                    url: Joi.string().allow(""),
                    type: Joi.number().required(),
                    icon: Joi.string().required(),
                    title: Joi.string().required(),
                });
                let { error } = ctx.validate(scm, item);
                if (error) {
                    ctx.status = 403;
                    return { error: error.message };
                }
            };
            await app.mysql.get('Yusup').update('settings', { setting_content: JSON.stringify(list) }, {
                where: {
                    user: app.dbName,
                    setting_name
                }
            });
            return {
                code: 200,
                data: 'success'
            }

        }
        //? 保存分类配置
        else if (setting_name == 'channelList') {
            let list = ctx.request.body.list || [];
            for (const item of list) {
                item.xh = Number(item.xh);
                let scm = Joi.object({
                    xh: Joi.number().required(),
                    title: Joi.string().required(),
                    thumb_url: Joi.string().required(),
                    url: Joi.string().required()
                });
                let { error } = ctx.validate(scm, item);
                if (error) {
                    ctx.status = 403;
                    return { error: error.message };
                }
            };
            await app.mysql.get('Yusup').update('settings', { setting_content: JSON.stringify(list) }, {
                where: {
                    user: app.dbName,
                    setting_name
                }
            });
            return {
                code: 200,
                data: 'success'
            }

        }
        //? 播放页广告配置
        else if (setting_name == 'appBaseConfig') {
            let { config } = ctx.request.body
            let schema = Joi.object({
                aboutPageUrl: Joi.string(),
                downloadUrl: Joi.string().required(),
                requestBaseUrl: Joi.string(),
                shareQrCoderUrl: Joi.string().required(),
                title: Joi.string(),
                version: Joi.string(),
                versionId: Joi.number(),
            });
            let { error } = ctx.validate(schema, config);
            if (error) {
                ctx.status = 403;
                return { error: error.message };
            };
            await app.mysql.get('Yusup').update('settings', { setting_content: JSON.stringify(config) }, {
                where: {
                    user: app.dbName,
                    setting_name
                }
            });
            return {
                code: 200,
                data: 'success'
            }
        }
    }
}